package com.pdg.mcplugin.spawnsurance.commandprocessors;

import java.util.Iterator;

import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.baseclasses.CommandProcessorBase;
import com.pdg.mcplugin.spawnsurance.SpawnSurance;

public class GetCommandProcessor extends CommandProcessorBase<SpawnSurance> {

	private static final String messageSuccess = "The value of '%s' is '%s'.";

	public GetCommandProcessor(SpawnSurance thePlugin) {
		super(thePlugin, new String[]{
				"/spawnsurance get (property) (parameter)",
				"Available \"get\" Properties:",
				"  claims - current player's number of claims",
				"  claims-for (player name) - number of claims for player",
				"  material - material for spawnsurance cost",
				"  minimum - minimum cost for spawnsurance",
				"  maximum - maximum cost for spawnsurance",
				"  acceleration - acceleration for spawnsurance cost",
				"  message-enabled - current state of spawnsurance message",
				"  message-text - custom text of spawnsurance message",
				"  filter-state - the state of the filter",
				"  filter-materials - the materials in the filter"
		});
		setSubProcessor(Property.CLAIMS_FOR.getText(), new GetClaimsForCommandProcessor(thePlugin));
	}

	@Override
	protected boolean specialProcessCommand(CommandSender theCommandSender,
			String theSubCommand, ArgumentList theArgumentList) {
		Property property = Property.parse(theSubCommand);
		if(property==Property.INVALID){
			return false;
		}
		if(!getPlugin().getPermissionChecker().checkPermission(theCommandSender, property.getGetPermission())){
			return false;
		}
		try{
			String theValue = null;
			switch(property){
			case ACCELERATION:
				theValue = getPlugin().getPluginConfiguration().getAcceleration().toString();
				break;
			case MATERIAL: 
				theValue = getPlugin().getPluginConfiguration().getMaterial().toString();
				break;
			case MAXIMUM: 
				theValue = getPlugin().getPluginConfiguration().getMaximum().toString();
				break;
			case MINIMUM: 
				theValue = getPlugin().getPluginConfiguration().getMinimum().toString();
				break;
			case MESSAGE_ENABLED: 
				theValue = new Boolean(getPlugin().getPluginConfiguration().getMessageEnabled()).toString();
				break;
			case MESSAGE_TEXT: 
				theValue = getPlugin().getPluginConfiguration().getMessageText();
				break;
			case PAY_SOURCE:
				theValue = getPlugin().getPluginConfiguration().getPaySource().toString();
				break;
			case CLAIMS:
				String thePlayerName = ((Player)theCommandSender).getName();
				theValue = getPlugin().getDataProvider().retrievePlayerClaims(thePlayerName).toString();
				break;
			case FILTER_STATE:
				theValue = getPlugin().getPluginConfiguration().getFilterState().toString();
				break;
			case FILTER_MATERIALS:
				theValue = "";
				Iterator<Material> iterator = getPlugin().getPluginConfiguration().getFilterMaterials().iterator();
				while(iterator.hasNext()){
					Material theMaterial = iterator.next();
					if(theValue.length()==0){
						theValue = theMaterial.toString();
					}else{
						theValue += ", " + theMaterial.toString();
					}
				}
				break;
			default:
				break;
			}
			getPlugin().sendSuccessMessage(theCommandSender, String.format(messageSuccess,property.getLabel(),theValue));
			return true;
		}catch(Exception e){
			return false;
		}
	}

}
